<?php
require_once "config.php";

$uid = $_POST['uid'];
$aid = $_POST['aid'];
$code = $_POST['code'];

if($code == 1){
    $favorite = $mysql->query("select * from dyspw_userfavorite where uid={$uid} and aid={$aid}");
    $favorite_rows = $favorite->fetch_all(MYSQLI_ASSOC);

    if($favorite_rows){
        $favorite_up = $mysql->query("update dyspw_userfavorite set isshow=1 where uid={$uid} and aid={$aid}");
        if($favorite_up){
            echo json_encode(['code'=>1]);
        }else{
            print_r("修改失败");die;
        }
    }else{
        $weblink = $mysql->query("select * from dyspw_weblink where id={$aid}");
        $weblink_rows = $weblink->fetch_all(MYSQLI_ASSOC);

        $user = $mysql->query("select * from dyspw_member where id={$uid}");
        $user_rows = $user->fetch_all(MYSQLI_ASSOC);
        if($weblink_rows and $user_rows){
            $now_time = time();
            $favorite_ins = $mysql->query("insert into dyspw_userfavorite (aid,uid,uname,link,isshow) values ('{$aid}','{$uid}','{$user_rows[0]['username']}','{$weblink_rows[0]['linkurl']}',1)");
            if( $favorite_ins ){
                echo json_encode(['code'=>1]);
            }else{
                print_r('添加失败');die;
            }
        }else{
            print_r("没有查询到视频详情和用户详情");die;
        }
    }



}else{
    $favorite = $mysql->query("select * from dyspw_userfavorite where uid={$uid} and aid={$aid}");
    $favorite_rows = $favorite->fetch_all(MYSQLI_ASSOC);

    if($favorite_rows){
        $favorite_up = $mysql->query("update dyspw_userfavorite set isshow=0 where uid={$uid} and aid={$aid}");
        if($favorite_up){
            echo json_encode(['code'=>1]);
        }else{
            print_r("修改失败");die;
        }
    }

}


?>
